const cloud = require('wx-server-sdk');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db = cloud.database();

// 获取openId云函数入口函数
exports.main = async (event, context) => {
  // 获取基础信息
  const wxContext = cloud.getWXContext();
  // const user = await db.collection('user').where({
  //   openId: wxContext.OPENID,
  // }).get()
  
  const aiNumber = await db.collection('aiNumber').where({
    openId: wxContext.OPENID,
  }).get()
  // console.log(aiNumber.data[0].number)
  if(aiNumber.data.length <= 0){
    await db.collection('aiNumber').add({
      // data 字段表示需新增的 JSON 数据
      data: {openId: wxContext.OPENID,number:5}
    });
  }
  const template = await db.collection('template')  .orderBy('_id', 'desc').limit(10).get()

  return {
    openid: wxContext.OPENID,
    aiNumber: aiNumber.data[0],
    template:template,
  };
};
